package com.douqu.game.auth.service;

import com.bean.core.util.Utils;
import com.douqu.game.auth.database.mapper.GMMapper;
import com.douqu.game.core.database.model.GMModel;
import com.douqu.game.core.e.E_GMType;
import com.douqu.game.core.e.ReturnMessage;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.core.web.response.BaseResponseDto;
import com.douqu.game.core.web.response.ErrorResponseDto;
import com.douqu.game.core.web.response.SuccessResponseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by bean on 2017/8/12.
 */
@Service
public class GMServiceImpl implements GMService {

    @Autowired
    private GMMapper gmMapper;


    @Override
    public int create(GMModel gmModel) {
        return gmMapper.insert(gmModel);
    }

    @Override
    public int openOrClose(int id) {
        return gmMapper.openOrClose(id);
    }

    @Override
    public BaseResponseDto update(GMModel gmModel)
    {
        GMModel nameHistory = gmMapper.findByName(gmModel.getUsername());
        GMModel idHistory = null;
        if(gmModel.getId() != null && gmModel.getId() != 0)
        {
            idHistory = gmMapper.findById(gmModel.getId());
        }
        if(gmModel.getType() == E_GMType.SUPER.getCode())
            gmModel.setDel(false);

        if(nameHistory == null)
        {
            if(idHistory != null)
            {
                return new ErrorResponseDto(ReturnMessage.GM_NO_UPDATE_USERNAME);
            }

            //新增
            gmMapper.insert(gmModel);
        }
        else
        {
            if(idHistory != null && nameHistory.getId() != idHistory.getId())
            {
                //用户名和ID不对应
                return new ErrorResponseDto(ReturnMessage.GM_NO_UPDATE_USERNAME);
            }

            //更新
            gmMapper.updateByName(gmModel);
        }

        return new SuccessResponseDto(gmMapper.findList());
    }

    @Override
    public List<GMModel> findList() {
        return gmMapper.findList();
    }

    @Override
    public BaseResponseDto findByName(String username)
    {
        GMModel gmModel = gmMapper.findByName(username);
        if(gmModel != null)
            return new SuccessResponseDto(gmModel);
        else
            return null;
    }

    @Override
    public GMModel findById(Integer id) {
        return gmMapper.findById(id);
    }

    @Override
    public BaseResponseDto login(String username, String password)
    {
        if(Utils.isNullOrEmpty(username) || Utils.isNullOrEmpty(password))
        {
            LogUtils.warn("GM登录参数错误 -> username:" + username + "  password:" + password);
            return new ErrorResponseDto(ReturnMessage.PARAM_ERROR);
        }
        GMModel gmModel = gmMapper.findByName(username);
        if(gmModel == null || !gmModel.getPassword().equals(password))
        {
            LogUtils.warn("GM登录账号或密码错误 -> username:" + username + "  password:" + password);
            return new ErrorResponseDto(ReturnMessage.PASSWORD_ERROR);
        }

        return new SuccessResponseDto(gmModel);
    }

}
